// pages/detail/detail.js
const {
  localUrl
} = require("../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    user: {},
    comment: {},
    index: {},
    nickname: '---',
    avatar: '',
    mediaList: [],
    active: 0,
    show: false,
  },

  //预览图片，放大预览
  preview(event) {
    console.log(event.currentTarget.dataset.src)
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.mediaList // 需要预览的图片http链接列表
    })
  },
  addComment(){
    wx.request({
      url: localUrl+'/label/comment/addComment',
      method:'POST',
      data:{
        userId:this.data.userInfo.id,
        labelId:this.data.id,
        comment:this.data.content
      },
      success(res){
        console.log("addComment",res)
      }
    })
    wx.showToast({
      title: '添加成功',
      icon:'success'
    })
    this.setData({
      show: false
    })
    wx.navigateTo({
      url: '/pages/detail/detail?id='+this.data.id,
     
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log("detail", options.id)
    this.setData({
      id: options.id
    })
    wx.request({
      url: localUrl + '/label/img/getImgList/' + that.data.id,
      method: 'GET',
      success(res) {
        console.log("*------------", res)
        that.setData({
          mediaList: res.data.data.imageList
        })
      }
    })
  },
  remarkInputAction:function(options){
    //获取输入框输入的内容
    let value = options.detail.value;
    console.log("输入框输入的内容是 " + value)
    this.setData({
      content:options.detail.value
    })
     },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    var that = this
    console.log(event.detail)
    
    if (event.detail == 1) {
      console.log(event.detail)
      this.setData({
        show: true
      })


    }
    else if (event.detail==0) {
      this.setData({
        'index.likes':that.data.index.likes+1
      })
      wx.request({
        url: localUrl+'/label/like/'+that.data.id,
        method:'GET',
        success(res){
          console.log(res)
          if (res.data.code==20000) {
            wx.showToast({
              title: '点赞成功',
              icon:'success'
            })
           
          }
        }
      })
    }
    this.setData({
      active: event.detail
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',

    })

    var that = this
    wx.request({
      url: localUrl + '/label/getLoabelById/' + this.data.id,
      method: 'GET',
      success(res) {
        console.log(res)
        that.setData({
          comment: res.data.data.comment,
          index: res.data.data.index,
          user: res.data.data.userinfo,
          avatar: res.data.data.userinfo.avatar,
          nickname: res.data.data.userinfo.nickname
        })
      }
    })
    wx.request({
      url: localUrl+'/ucenter/member/getMemberInfo',
      method:'GET',
      header:{
       'content-type': 'application/json', // 默认值
        'token':wx.getStorageSync('token')
      },
      
      success(res){
        console.log("token",res)
        that.setData({
          userInfo:res.data.data.userInfo
        })
      }
    })

    setTimeout(function () {
      wx.hideLoading({
          success: (res) => {},
        }),
        2000
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})